
pkgname=shadow
pkgver=4.5
pkgrel=1
pkgdesc="Password and account management tool suite with support for shadow files and PAM"
arch=('x86_64')
url='https://github.com/shadow-maint/shadow'
license=('BSD')
groups=('base')
depends=('bash' 'pam' 'acl')
makedepends=('gnome-doc-utils')
backup=(etc/login.defs
        etc/pam.d/{chage,passwd,shadow,useradd,usermod,userdel}
        etc/pam.d/{chpasswd,newusers,groupadd,groupdel,groupmod}
        etc/pam.d/{chgpasswd,groupmems}
        etc/default/useradd)
options=('!libtool')
install='shadow.install'
source=("https://github.com/shadow-maint/shadow/archive/${pkgver}.tar.gz"
        'LICENSE'
        'chgpasswd'
        'chpasswd'
        'defaults.pam'
        'login.defs'
        'newusers'
        'passwd'
        'shadow.service'
        'shadow.timer'
        'useradd.defaults'
        'xstrdup.patch'
        'shadow-strncpy-usage.patch')
md5sums=('05712082d585048240c4a0251d3a5062'
         'bdf93c7bc4b751277159c5de8fc34df2'
         '65e9ebce249a5b9ed021e2790452b9e1'
         'f6760e9701d7ed2cd91371d427faed2a'
         'a31374fef2cba0ca34dfc7078e2969e4'
         'c4db3dbd4d5759d3e4f29366946d14e5'
         'f6760e9701d7ed2cd91371d427faed2a'
         'cb4556b8d92d5d09a690ce06c57b0f76'
         '23df9e8f5746c2679bc1f276388eac18'
         '5bd14862ace022d6682b179c18fe30de'
         '352d2524bfa911d1c50c8fcef57d3acc'
         '0eebe9d13065bec4b5d7ccf3bf46c509'
         '8a9152e7448a0f9d1d07ed0016776e16')

build() {
  cd ${pkgname}-${pkgver}

  # avoid transitive linking issues with binutils 2.22
  sed -i '/^user\(mod\|add\)_LDADD/s|$| -lattr|' src/Makefile.am

  # link to glibc's crypt(3)
  LDFLAGS+=" -lcrypt"

  patch -p1 -i ${srcdir}/xstrdup.patch
  patch -p1 -i ${srcdir}/shadow-strncpy-usage.patch

  # supress etc/pam.d/*
  sed -i '/^SUBDIRS/s/pam.d//' etc/Makefile.am
  
  sed -i 's|--without-libpam|--with-libpam|' autogen.sh
  sed -i 's|--with-selinux|--without-selinux|' autogen.sh

  ./autogen.sh
  ./configure \
    --prefix=/usr \
    --libdir=/lib \
    --mandir=/usr/share/man \
    --sysconfdir=/etc \
    --with-libpam \
    --with-group-name-max-length=32 \
    --with-selinux=no \
    --enable-man

  make
}

package() {
  cd ${pkgname}-${pkgver}

  make DESTDIR=${pkgdir} install

  # license
  install -Dm644 ${srcdir}/LICENSE ${pkgdir}/usr/share/licenses/shadow/LICENSE

  # useradd defaults
  install -Dm644 ${srcdir}/useradd.defaults ${pkgdir}/etc/default/useradd

  # systemd timer
  install -D -m644 ${srcdir}/shadow.timer ${pkgdir}/usr/lib/systemd/system/shadow.timer
  install -D -m644 ${srcdir}/shadow.service ${pkgdir}/usr/lib/systemd/system/shadow.service
  install -d -m755 ${pkgdir}/usr/lib/systemd/system/multi-user.target.wants
  ln -s ../shadow.timer ${pkgdir}//usr/lib/systemd/system/multi-user.target.wants/shadow.timer

  # login.defs
  install -Dm644 ${srcdir}/login.defs ${pkgdir}/etc/login.defs

  # PAM config - custom
  install -dm755 ${pkgdir}/etc/pam.d
  install -t ${pkgdir}/etc/pam.d -m644 ${srcdir}/{passwd,chgpasswd,chpasswd,newusers}

  # PAM config - from tarball
  install -Dm644 etc/pam.d/groupmems ${pkgdir}/etc/pam.d/groupmems

  # we use the 'useradd' PAM file for other similar utilities
  for file in chage groupadd groupdel groupmod shadow \
      useradd usermod userdel; do
    install -Dm644 ${srcdir}/defaults.pam ${pkgdir}/etc/pam.d/$file
  done

  # Remove utilities provided by util-linux
  rm \
      ${pkgdir}/usr/bin/{chsh,chfn,sg} \
      ${pkgdir}/bin/{login,su} \
      ${pkgdir}/usr/sbin/{vipw,vigr} \
      ${pkgdir}/sbin/nologin

  # but we keep newgrp, as sg is really an alias to it
  mv ${pkgdir}/usr/bin/{newgrp,sg}

  # ...and their many man pages
  find ${pkgdir}/usr/share/man \
      '(' -name 'chsh.1'  -o \
          -name 'chfn.1'  -o \
          -name 'su.1'    -o \
          -name 'login.1' -o \
          -name 'nologin.8' -o \
          -name 'vipw.8'  -o \
          -name 'vigr.8'  -o \
          -name 'newgrp.1' ')' \
      -delete
  rmdir \
      ${pkgdir}/usr/share/man/{fi,id,zh_TW}/man1 \
      ${pkgdir}/usr/share/man/{fi,ko/man8}
}
